package com.xgk.boot.framework.common.util.threadpool;

import cn.hutool.core.thread.ThreadUtil;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

public class ThreadPoolManager {

    // 私有化构造方法
    private ThreadPoolManager() {}

    /**
            * 创建固定大小的线程池
     * @param poolSize 线程池大小
     * @param poolName 线程池名称前缀
     * @return ExecutorService
     */
    public static ExecutorService newFixedPool(int poolSize, String poolName) {
        return new ElmThreadPoolExecutor(poolSize, poolSize, 60L, TimeUnit.SECONDS,new LinkedBlockingQueue<>(),
                ThreadUtil.newNamedThreadFactory(poolName, true));
    }

    /**
            * 创建可伸缩的线程池
     * @param corePoolSize 核心线程数
     * @param maxPoolSize 最大线程数
     * @param poolName 线程池名称前缀
     * @return ExecutorService
     */
    public static ExecutorService newFlexiblePool(int corePoolSize, int maxPoolSize, String poolName) {
        return new ElmThreadPoolExecutor(
                corePoolSize,
                maxPoolSize,
                60L, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(),
                ThreadUtil.newNamedThreadFactory(poolName, true),
                new ThreadPoolExecutor.AbortPolicy()
        );
    }

     /**
             * 创建CPU密集型任务线程池
     */
    public static ExecutorService newCPUPool(String poolName) {
        int poolSize = getIdealThreadCount();
        return new ElmThreadPoolExecutor(
                poolSize,
                poolSize,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(),
                new NamedThreadFactory(poolName)
        );
    }

    /**
            * 创建IO密集型任务线程池
     */
    public static ExecutorService newIOPool(String poolName) {
        int poolSize = getIdealThreadCount() * 2;
        return new ElmThreadPoolExecutor(
                poolSize / 2,  // 核心线程数
                poolSize,     // 最大线程数
                60L, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(1000),
                new NamedThreadFactory(poolName),
                new ThreadPoolExecutor.CallerRunsPolicy()
        );
    }

    /**
            * 获取CPU核心数（理想线程数基础）
            */
    public static int getIdealThreadCount() {
        return Runtime.getRuntime().availableProcessors();
    }

    /**
            * 自定义线程工厂（带名称和守护线程设置）
            */
    static class NamedThreadFactory implements ThreadFactory {
        private final String namePrefix;
        private final boolean daemon;
        private final AtomicInteger threadNumber = new AtomicInteger(1);

        NamedThreadFactory(String namePrefix) {
            this(namePrefix, false);
        }

        NamedThreadFactory(String namePrefix, boolean daemon) {
            this.namePrefix = namePrefix;
            this.daemon = daemon;
        }

        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread(r, namePrefix + "-thread-" + threadNumber.getAndIncrement());
            t.setDaemon(daemon);
            return t;
        }
    }
}